#!/usr/bin/perl
#index.cgi

require "./mdmhistory-lib.pl";

&header($text{'index_title'}, "", "index", 0, 1);

&foreign_require("mdmmod","mdmmod-lib.pl");
local $mdmmod=&foreign_call("mdmmod","get_mdm_mod");
if ($mdmmod ne 'server'){
	print "$text{'deny'}\n";
	&footer("/",$text{'index'});
	exit;
}

if (!(-f $config{'mdmlogfile'})){
	`mkdir -p $config{'mdmlogdir'}`;
	`touch $config{'mdmlogfile'}`;
}

my $mode=2;
my %smonth= ('Jan',1, 'Feb',2, 'Mar',3, 'Apr',4,  'May',5,  'Jun',6,
             'Jul',7, 'Aug',8, 'Sep',9, 'Oct',10, 'Nov',11, 'Dec',12 );

open(FILE,$config{"mdmlogfile"})||die "open failed: $!\n";
my @file=<FILE>;
print "<table width=100%>\n";
print "<tr><td>\n";
print "<div class=main>"; 
print "<table class=mode1 border=0 cellspacing=0 cellpadding=0 width='100%'>\n";
print "<tr class=title1>\n";
print "<td><b>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$text{'table_date'}</b></td>\n";
print "<td><b>$text{'table_time'}</b></td>\n";
print "<td><b>$text{'table_event'}</b></td>\n";
print "</tr>\n";
print "<form method=post onsubmit='return onsubmit_form();'>\n";
my $disable='disabled=true';
my $i=0;
for($j=0;$j<@file;$j++){
	if ($file[$j] =~ /^\[TIME -/){
		chomp($file[$j]);
		$file[$j] =~ s/^\[TIME -\s*//;
		my ($year,$month,$date,$time)=split(/ /,$file[$j]);
		$time =~s/\]//;
		if(! defined $smonth{$month} ) { next; } 
		$j++;
		if ($file[$j] != /^\[OPERATE -/){ next; }
		chomp($file[$j]);
		$file[$j] =~ s/^\[OPERATE - //;
		my $event = $file[$j];	
		$event =~ s/\]//;	
		$mode = ( $mode==2 ? 1:2 );
		$i++;
		print "<tr class=mode$mode>\n";
		printf "<td><input type=radio %s value=$i name=line_no>\n",($i == 1)?'checked':'';
		printf "&nbsp;%d-%02d-%02d</td>\n",$year,$smonth{$month},$date;
		print "<td>",$time,"</td>";
		print "<td>",$event,"</td>";
		print "</tr>\n";
		$disable=' ';
	}
}
close(FILE);

print "</table>";
print "</div>";
print "</td>";

print "<td valign=top width=120>";
print "<table width=100% cellpadding=4>";

print "<input type=hidden name=log value='$logname'>";
print "<tr><td>";
print "<input type=submit name=event class=button ";
print " value='$text{'event_details'}' onclick='read_log();' $disable>";
print "</td></tr>";

print "<tr><td>";
print "<input type=submit name=download class=button ";
print " value='$text{'download_log'}' onclick='download_log();'>";
print "</td></tr>";

print "<tr><td>";
print "<input type=submit name=setup class=button ";
print " value='$text{'log_properties'}' onclick='log_properties();'>";
print "</td></tr>";

print "<tr><td>";
print "<input type=submit name=clear class=button ";
print " value='$text{'clear_log'}' onclick='clear_log();' $disable>";
print "</td></tr>";
print "</form>";

print "<tr height=100%><td>&nbsp;</td></tr>";

print "</table>";
print "</td>";
print "</tr>";
print "</table>";

print <<EOF;
<script>
function log_properties()
{
	document.forms[0].action="config_log.cgi";
}

function download_log()
{
	document.forms[0].action="download_log.cgi";
}

function read_log()
{
	document.forms[0].action="read_log.cgi";
}

function clear_log()
{
	if (!confirm("$text{'clear_log_confirm'}")) return false;
	document.forms[0].action="delete_log.cgi";
}
function onsubmit_form()
{
        document.forms[0].event.disabled = true;
        document.forms[0].download.disabled = true;
        document.forms[0].setup.disabled = true;
        document.forms[0].clear.disabled = true;
}

</script>
EOF

&footer("/",$text{'index'});